package com.zkca.xf.ui.activity.personprotect;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zkca.xf.ConstantsXF;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.MesgLData;
import com.zkca.xf.model.javabean.uploadbean.Mesg_UP;
import com.zkca.xf.presenter.MesgReplyPresenter;
import com.zkca.xf.ui.activity.webview.MyWebViewActivity;
import com.zkca.xf.utils.DeviceUtils;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.Md5Util;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.StringUtils;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.MesgReplyView;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MesgXActivity extends BaseTitleActivity implements MesgReplyView {

    @BindView(R.id.mesgtitle)
    TextView mesgtitle;
    @BindView(R.id.cometime)
    TextView cometime;
    @BindView(R.id.come)
    TextView come;
    @BindView(R.id.info)
    TextView info;
    @BindView(R.id.image1)
    ImageView image1;
    @BindView(R.id.url_lianjie)
    TextView urlLianjie;
    @BindView(R.id.url_linear)
    LinearLayout urlLinear;
    @BindView(R.id.huifuinfo)
    EditText huifuinfo;
    @BindView(R.id.beganup)
    Button beganup;

    private Intent intent;
    private String isImage = "0", isFujian = "0", isLianjian = "0";
    private MesgReplyPresenter mesgReplyPresenter;
    private Mesg_UP mesg_up;
    private int notifyId;
    private String notifyResult;

    private String mobileToken;

    private String mobileType="1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mesg_x);
        ButterKnife.bind(this);
        initMyView();
        mesgReplyPresenter = new MesgReplyPresenter(this, this);

    }

    private void initData() {
        Map<String, Object> param = new HashMap<String, Object>();
        mesg_up = new Mesg_UP();
        param.put("notifyId", notifyId);
        if (!StringUtils.isEmpty(huifuinfo.getText())){
            param.put("notifyResult",huifuinfo.getText()+"");
        }
        mesg_up.setParam(param);
        mesg_up.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        mesg_up.setTimeStamp(TimeUtils.getTime());
        mesg_up.setSign(generateSign(mesg_up));
        mesgReplyPresenter.ReplyNesage(mesg_up);
    }
    public static String generateSign(Mesg_UP req) {
        String result = null;
        StringBuilder sb = new StringBuilder();
        sb.append("#");
        sb.append("Tqp5$%HzzYrP#!00098yyYL-");
        sb.append("#");
        sb.append(req.getAccessId() + req.getAccessId());
        sb.append("#");
        result = sb.toString();
        for (int k = 0; k < 4; k++) {
            result = Md5Util.md5Sum(result);
        }
        return result;
    }

    private void initMyView() {
        setTitleText("消息管理");
        setleftTextVisibility(true);
        setleftImgVisibility(true);
        intent = getIntent();
        final MesgLData.DataBean dataBean = (MesgLData.DataBean) intent.getSerializableExtra("mesgBean");
        String title = dataBean.getTitle();//标题
        long notifyDate = dataBean.getRecord().getNotifyDate();
        String formatDate2 = TimeUtils.getFormatDate2(notifyDate);//xiada
        String content = dataBean.getContent();//内容
        String files = dataBean.getFiles();
        notifyId = dataBean.getRecord().getNotifyId();


        mesgtitle.setText(title);
        cometime.setText(formatDate2);
        come.setText(dataBean.getName1());
        info.setText(content);


        String notifyStatus = dataBean.getRecord().getNotifyStatus();
        LogUtil.e("1212",notifyStatus);
        int type1 = dataBean.getType1();
        if (type1 == 1) {
            huifuinfo.setVisibility(View.GONE);
            beganup.setVisibility(View.GONE);
        }else if (type1==2){
            if ("3".equals(notifyStatus)){
                beganup.setText("已回复");
                beganup.setClickable(false);
                beganup.setBackgroundResource(R.drawable.red_bg_hui);
                huifuinfo.setText(dataBean.getRecord().getNotifyReasult());
                huifuinfo.setFocusable(false);

                huifuinfo.setFocusableInTouchMode(false);
            }else if ("4".equals(notifyStatus)){
                beganup.setText("已过期");
                beganup.setClickable(false);
                beganup.setBackgroundResource(R.drawable.red_bg_hui);
                huifuinfo.setText(dataBean.getRecord().getNotifyReasult());
                huifuinfo.setFocusable(false);

                huifuinfo.setFocusableInTouchMode(false);
            }else if ("2".equals(notifyStatus)){

                beganup.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        beganup.setClickable(false);

                        initData();
                    }
                });
            }
        }



        if (files != null) {
            char[] chars = files.toCharArray();
            isImage = String.valueOf(chars[0]);
            isFujian = String.valueOf(chars[1]);
            isLianjian = String.valueOf(chars[2]);
        }

        if (isLianjian.equals("0")) {
            urlLinear.setVisibility(View.GONE);
        } else {
            urlLianjie.setText(dataBean.getFiles4());
            urlLianjie.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
            urlLianjie.getPaint().setAntiAlias(true);//抗锯齿
            urlLinear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(mContext, MyWebViewActivity.class);
                    intent.putExtra("title","通知");
                    intent.putExtra("url",dataBean.getFiles4());
                    startActivity(intent);
                }
            });
        }
        if (isImage.equals("0")) {
            image1.setVisibility(View.GONE);
        } else {
            Glide.with(this).load(ConstantsXF.URL_SERVER_CE + dataBean.getFiles2()).asBitmap().placeholder(R.mipmap.zwf).error(R.mipmap.zwf).into(image1);
        }
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {
        ToastUtils.show(mContext,msg);
        startActivity(new Intent(mContext,MesActivity.class));
        finish();
    }

    @Override
    public void ReplyMesg(MesgLData s) {
        ToastUtils.show(mContext,s.getInfo());
        startActivity(new Intent(mContext,MesActivity.class));
        finish();
    }
}
